package admin;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.NoSuchElementException;
import java.util.Scanner;
import javax.xml.rpc.ServiceException;

import COMA_API.*;


public class Coma {

	public static void main(String[] args) {
		if(args.length != 2){
			System.out.println("Usage: Coma <web service URL> -A | <import file>\n" +
					"\t-A\tView agreement.");
		}
//		System.setProperty("log4j.configuration", "log4j.properties");

		if(args[1].equals(Constants.VIEW_AGREEMENT)){

			try {

				// Connect to web service
				ServiceCOMA_APISoap service = new ServiceCOMA_APILocator().getServiceCOMA_APISoap(new URL(args[0]));
				System.out.println("Connected to web service.");
				viewAgreement(service);

			} catch (IOException e) {
				e.printStackTrace();
			} catch (ServiceException e) {
				e.printStackTrace();
			}

		} else {
			try {
				// Read all lines into memory
				BufferedReader reader = new BufferedReader(new FileReader(args[1]));
				ArrayList<String> allLines = new ArrayList<String>(1000);
				for(String line = reader.readLine(); line != null; line = reader.readLine()){
					line = line.trim();
					if( line.startsWith("#") || line.length() == 0){
						// Do nothing.
					} else if(line.startsWith(Constants.IMPORT_CONTENT_AGREEMENT)){
						validateLine(line, 10, 10);
						allLines.add(line);
					} else if(line.startsWith(Constants.CONFIGURE_AGREEMENT)){
						validateLine(line, 3, 100);
						allLines.add(line);
					} else if(line.startsWith(Constants.ADD_PARTY)){
						validateLine(line, 8, 8);
						allLines.add(line);
					} else {
						throw new ParserException("Error invalid line: " + line);
					}
				}
				reader.close();
				System.gc();
				
				// Connect to web service
				ServiceCOMA_APISoap service = new ServiceCOMA_APILocator().getServiceCOMA_APISoap(new URL(args[0]));
				System.out.println("Connected to web service.");
	
				// Process import
				ImportFile importFile = new ImportFile(service, allLines);
				importFile.processImport();
				System.out.println("Import successfully ended.");
	
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (ParserException e) {
				e.printStackTrace();
			} catch (ServiceException e) {
				e.printStackTrace();
			}
		}
	}

	static void validateLine(String line, int minExpectedPipes, int maxExpectedPipes) throws ParserException{
		Scanner scanner = new Scanner(line);
		scanner.useDelimiter("\\|");
		int pipes = 0;
		try{
			while(true){
				scanner.next();
				pipes++;
			}
		} catch(NoSuchElementException  e){
		}
		pipes--;
		if(pipes < minExpectedPipes || pipes > maxExpectedPipes){
			throw new ParserException("Invalid numer of pipes, got " + pipes
				+ " min allowed " + minExpectedPipes + " max allowed " + maxExpectedPipes);
		}
	}
	
	static void viewAgreement(ServiceCOMA_APISoap service) throws RemoteException{
		Agreement[] allAgreements = (Agreement[])service.agreement_GET_LIST(1, 1, 100000).getComaobject();
		Party[] allRelatedParties = null;
		Rights[] allRelatedRights = null;
		for(int i = 0; i < allAgreements.length; i++){
			System.out.println("Agreement: (" + allAgreements[i].getId() + ")" + allAgreements[i].getName());

			System.out.println("\tRights: ");
			allRelatedRights = (Rights[])service.rights_GET_LIST_BY_AGREEMENT_ID(1, allAgreements[i].getId(), 1, 10000).getComaobject();
			for(int n = 0; n < allRelatedRights.length; n++){
				System.out.println("\t\t(" + allRelatedRights[n].getId() + ")" + allRelatedRights[n].getName());
			}
			if(allRelatedRights.length == 0){
				System.out.println("\t\t<< No related Rights >>");
			}

			System.out.println("\tParties: ");
			allRelatedParties = (Party[])service.party_GET_LIST_BY_AGREEMENT_ID(1, allAgreements[i].getId(), 1, 10000).getComaobject();
			for(int n = 0; n < allRelatedParties.length; n++){
				System.out.println("\t\t(" + allRelatedParties[n].getId() + ")" + allRelatedParties[n].getName());
			}
			if(allRelatedParties.length == 0){
				System.out.println("\t\t<< No related Party >>");
			}

			System.out.println(""); // Line break
		}
		
	}
}
